import { useState, useEffect, useCallback } from "react";
import { message } from "antd";
import { getTextInfo } from "@/api";

const useJokes = (initPage = 1, initMaxResult = 20) => {
  const [page, setPage] = useState(initPage);
  const [maxResult, setMaxResult] = useState(initMaxResult);
  const [responseData, setResponseData] = useState([]);
  const getJokesData = useCallback(async () => {
    const responseData = await getTextInfo({
      page,
      maxResult,
      showapi_appid: "108178",
      showapi_sign: "198758c24c74470ca34372f2447268e4",
    });
    if (responseData.showapi_res_code === 0) {
      setResponseData(responseData.showapi_res_body);
    } else message.error(responseData.showapi_res_error);
  }, [maxResult, page]);
  useEffect(() => getJokesData(), [page, maxResult, getJokesData]);

  return [page, setPage, responseData, maxResult, setMaxResult];
};

export default useJokes;
